package lib.operation;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @author FBI
 * 2022-02-06 18:22
 */

//删，通过书名删除书
public class Del implements Do {

    @Override
    public void work() {
        System.out.println("=====删除书籍=====");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入书名");
        String name = sc.nextLine();  //name为所要删除的书名

        Del.delBook(name);
    }

    public static void delBook(String name){
        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/lib","root","2506642408");
            stmt = conn.createStatement();
            String sql = "delete from book where name = "+"'"+ name + "'";
            int count = stmt.executeUpdate(sql);
            System.out.println(count == 1 ? "删除成功" : "删除失败");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
